package util;

import model.XML;
import enums.Symbols;

public final class Utils {
    /**
     * Don't let anyone instantiate this class.
     */
    private Utils() {}
    
	public static String formatNode(XML node, int level, boolean isClosedTag){
		String attr = "";
		String slash = "";
		String text = node.getText();
		if (isClosedTag){
			slash = Symbols.SLASH.getValue();
			text = "";
			attr = Symbols.SPACE.getValue() + node.getAttributes().toString();
		}
		
		return new StringBuilder().
				append( multiplyTabs(level) ).
				append(Symbols.LT.getValue()).
				append(slash).
				append(node.getName()).
				append(attr).
				append(Symbols.GT.getValue()).
				append(text).
				append(Symbols.BREAK_LINE.getValue()).
				toString();
	}
	
	/**
	 * Filter xml file before processing
	 * @param string plain xml text
	 * @return String
	 */
	public static String removeRedundantChars(String string) {
		return string
				.replaceAll("<\\?.*\\?>", "")			// header
				.replaceAll("(?mdx)<!--.*-->", "")		// comments
				.replaceAll("\r|\n|\t", "");			// line breaks
		
	}
	
	private static String multiplyTabs(int count){
		StringBuilder buf = new StringBuilder();
		
		for (int i = 0; i < count; i++) {
			buf.append(Symbols.TAB.getValue());
		}
		return buf.toString();
	}
	
	

}
